package com.heima.homeservicemanagementsystem.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.homeservicemanagementsystem.entity.ServiceClassification;
import com.heima.homeservicemanagementsystem.entity.dto.Option;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import  com.heima.homeservicemanagementsystem.module.PageParams;
import  com.heima.homeservicemanagementsystem.module.Result;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import com.heima.homeservicemanagementsystem.entity.CommentSensitiveText;
import com.heima.homeservicemanagementsystem.service.CommentSensitiveTextService;
import java.util.List;
import java.util.Map;

import static cn.dev33.satoken.SaManager.log;

/**
 * 屏蔽的敏感字段表;(comment_sensitive_text)表控制层
 * @author : http://www.chiner.pro
 * @date : 2025-4-21
 */
@Tag(name = "屏蔽的敏感字段表对象功能接口")
@RestController
@RequestMapping("/commentSensitiveText")
public class CommentSensitiveTextController{

    @Resource
    private CommentSensitiveTextService commentSensitiveTextService;
    @SaIgnore
    @Operation(summary = "服务父级下拉列表")
    @GetMapping("/serviceParent")
    public Result<List<Option>> roleOptions() {
        //查询参数

        QueryWrapper<CommentSensitiveText> lqw = new QueryWrapper<>();
        lqw.select("sensitive_text AS label", "id AS value");

        List<Map<String, Object>> maps = commentSensitiveTextService.listMaps(lqw);

        List<Option> optionals = BeanUtil.copyToList(maps, Option.class);
        return Result.success(optionals);
    }

    @Operation(summary="分页查询")
    @PostMapping("/page")
    public Result<Page<CommentSensitiveText>> paginQuery(@RequestBody PageParams<CommentSensitiveText> pageParams){
        //查询参数
        CommentSensitiveText params =pageParams.getParams();
        LambdaQueryWrapper <CommentSensitiveText> lqw =new LambdaQueryWrapper<>();



        return Result.success(commentSensitiveTextService.page(pageParams.getPage(),lqw));
    }


    @Operation(summary = "屏蔽的敏感字段表所有搜索")
    @GetMapping("/list")
    public Result searchQuery() {
        return Result.success(commentSensitiveTextService.list());
    }
    @Operation(summary = "有条件的搜索")
    @PostMapping("/tjSearch")
    public Result Query(@RequestBody CommentSensitiveText  commentSensitiveText) {
        LambdaQueryWrapper<CommentSensitiveText> lqw = new LambdaQueryWrapper<>();


        lqw.eq(commentSensitiveText.getId()!=null,CommentSensitiveText::getId,commentSensitiveText.getId());
        lqw.like(commentSensitiveText.getSensitiveText()!=null,CommentSensitiveText::getSensitiveText,commentSensitiveText.getSensitiveText());



        return Result.success(commentSensitiveTextService.list(lqw));
    }

    /**
     * 新增数据
     *
     * @param commentSensitiveText 实例对象
     * @return 实例对象
     */
    @Operation(summary="新增数据")
    @PostMapping("/insert")
    public Result<Boolean> insert(@RequestBody CommentSensitiveText commentSensitiveText){
        if(commentSensitiveTextService.save(commentSensitiveText)){
            return Result.success();
        }
        return Result.error();
    }

    /**
     * 更新数据
     *
     * @param commentSensitiveText 实例对象
     * @return 实例对象
     */
    @Operation(summary="更新数据")
    @PutMapping("update")
    public Result<Boolean> update(@RequestBody CommentSensitiveText commentSensitiveText){
        if(commentSensitiveTextService.updateById(commentSensitiveText)){
            return Result.success();
        }
        return Result.error();
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Operation(summary="通过主键删除数据",
            parameters={
                    @Parameter(name ="id",description="主键",required =true,in=ParameterIn.PATH)
            })
    @DeleteMapping("/delete/{id}")
    public Result<Boolean> deleteById(@PathVariable String id){

        if(commentSensitiveTextService.removeById(id)){
            return Result.success();
        }
        return Result.error();
    }

    @Operation(summary = "多id删除数据")
    @DeleteMapping("/delete")
    public Result<Boolean> deleteByIds(@RequestBody List<String> id) {
        log.info("前端信息：{}", id);
        if (commentSensitiveTextService.removeByIds(id)) {
            return Result.success();
        }
        return Result.error();
    }
}